package cn.com.callback

import cn.com.kh.KHUtil
import org.json.JSONArray
import org.json.JSONObject
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import java.text.SimpleDateFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.kh.getProperty;
/**
 *
 * @Title: aipHSTApplyMoreUsersTask
 * @Description:华盛证券 服务顾问申请更多客户请求 写入临时表 --内部接口
 * @author: chieh
 * @date 2023-01-12 12:32:00
 *
 */

public class aipHSTApplyMoreUsersTask implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(aipHSTApplyMoreUsersTask.class);
    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");

        String getMethod = request.getMethod();

        log.debug("getMethod:" + getMethod);
        long endTime ;
        String inData = "";
        String outData = "";
        String dataSourceID = "dataSource";

        BufferedReader iny = null;
        if (getMethod.equals("GET")) {
            inData = request.getParameter("data");
            log.debug("原始Get请求参数:" + inData);
        } else {
            InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
            iny = new BufferedReader(reader);
            String line;
            while ((line = iny.readLine()) != null) {
                inData += line;
            }
            log.debug("原始Post请求参数:" + inData);
        }

        try {
            long startTime = System.currentTimeMillis();
			
			// 调用处理信息
			JSONObject inObject = new JSONObject(inData);
			
			def rolegroup_id = GetJV(inObject, "rolegroup_id");//申请员工组ID
			def staff_id = GetJV(inObject, "staff_id");//申请员工ID
			def limit_num = GetJV(inObject, "limit_num");//当前申请配置的限额
			// 调用处理信息
			log.debug("解析信息-" + "rolegroup_id: " + rolegroup_id+ "staff_id: " + staff_id+ "limit_num: " + limit_num);
			
			//查询 当前是否已经申请过了
			String selecttaskSQL = "select rec_id from hst_apply_moreusers_task where state=0 and staff_id='" + staff_id + "' and rolegroup_id='" + rolegroup_id + "'";
			List selecttaskList = eliteBean.chaxun_sql(selecttaskSQL, "dataSource");
			
			//如没有 则insert
			if (selecttaskList.size() ==0) {
				String insertTaskSQL = "insert into hst_apply_moreusers_task(`assign_date`, `rolegroup_id`, `staff_id`, `limit_num`, `assign_num`, `state`, `last_check_time`) VALUES(Date_Format(Now(), '%Y-%m-%d'),'" + rolegroup_id + "','" + staff_id + "','" + limit_num + "',0,0,now())";
				eliteBean.update(insertTaskSQL, "dataSource");
				
				outData = "{\"resultcode\":\"1\",\"resultmessage\":\"申请更多客户任务提交成功！\",\"data\":[]}";
            }else{
				log.error("重复提交!");
                endTime = System.currentTimeMillis();
                return "{\"resultcode\":\"-1\",\"resultmessage\":\"申请更多客户任务提交失败(重复提交!),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
			}

        } catch (Exception e) {
            log.error("接口处理异常" + e.getMessage());
            outData = "{\"resultcode\":\"0\",\"resultmessage\":\"申请更多客户任务提交失败(" + e.getMessage() + ")\",\"data\":[]}";
        }

        return outData;
    }
	
	// 解析Json值
		public static String GetJV(JSONObject inJson, String getKey) {
			String Rev_Value = null;
			try {
				Rev_Value = inJson.getString(getKey);
			} catch (Exception e) {
				try {
					Rev_Value = String.valueOf(inJson.getInt(getKey));
				} catch (Exception e1) {
					try {
						Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
					} catch (Exception e2) {
						try {
							Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
						} catch (Exception e3) {
							Rev_Value = "";
						}
					}
				}
			} finally {
				if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
					Rev_Value = "";
				} else {
				}
			}
			log.debug("解析-k=" + getKey + " v=" + Rev_Value);
			return Rev_Value;
		}

}
